﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Explorer;
using SPVSX.Explorer.Extensions.Properties;
using SPVSX.Explorer.Extensions.NodeInformation;

namespace SPVSX.Explorer.Extensions
{
    /// <summary>
    /// Defines a new node type that represents a Style Library on a SharePoint site.
    /// </summary>
    // Enables Visual Studio to discover and load this extension.
    [Export(typeof(IExplorerNodeTypeProvider))]
    // Specifies the ID for this new node type.
    [ExplorerNodeType(StyleLibraryNodeTypeProvider.StyleLibraryNodeTypeId)]
    class StyleLibraryNodeTypeProvider
    {
        #region Constants

        /// <summary>
        /// The node type id
        /// </summary>
        internal const string StyleLibraryNodeTypeId = "SPVSX.SharePoint.StyleLibrary";

        #endregion

        #region Methods

        /// <summary>
        /// Initialise the node with its icon and event hookups
        /// </summary>
        /// <param name="typeDefinition">The node type defintion</param>
        public void InitializeType(IExplorerNodeTypeDefinition typeDefinition)
        {
            typeDefinition.DefaultIcon = Resources.ThemesNode.ToBitmap();
            typeDefinition.IsAlwaysLeaf = true;

            typeDefinition.NodePropertiesRequested += NodePropertiesRequested;
            typeDefinition.NodeMenuItemsRequested += NodeMenuItemsRequested;
            typeDefinition.NodeChildrenRequested += NodeChildrenRequested;
        }

        /// <summary>
        /// Creates the new web part nodes with the specified icon.
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The ExplorerNodeEventArgs object</param>
        private void NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
           
            
        }


        private void NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
        {
            e.MenuItems.Add(Resources.WebPartGalleryNode_ViewInBrowser, 5).Click += new EventHandler<MenuItemEventArgs>(WebPartNodeTypeProvider_Click);
        }

        void WebPartNodeTypeProvider_Click(object sender, MenuItemEventArgs e)
        {
        }

        /// <summary>
        /// Retrieves properties that are displayed in the Properties window when
        /// a web part gallery node is selected.
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The ExplorerNodePropertiesRequestedEventArgs object</param>
        private void NodePropertiesRequested(object sender,
            ExplorerNodePropertiesRequestedEventArgs e)
        {
            //var webPartGalleryNodeInfo = e.Node.GetAnnotation<WebPartGalleryNodeInfo>();

            //// Call the custom SharePoint command to get the Web Part properties.
            //Dictionary<string, string> properties =
            //    e.Node.Context.SharePointConnection.ExecuteCommand<
            //    WebPartGalleryNodeInfo, Dictionary<string, string>>(
            //    WebPartGalleryCommandIds.GetWebPartGalleryProperties, webPartGalleryNodeInfo);

            //object propertySource = e.Node.Context.CreatePropertySourceObject(properties);
            //e.PropertySources.Add(propertySource);
        }

        /// <summary>
        /// Creates all of the individual style nodes under the style library node.
        /// </summary>
        /// <param name="parentNode">The parent node</param>
        internal static void GetStyles(IExplorerNode parentNode)
        {
            //// Call the custom SharePoint command to get items from the Theme gallery.
            //var webParts = parentNode.Context.SharePointConnection.ExecuteCommand<WebPartNodeInfo[]>(
            //    WebPartGalleryCommandIds.GetWebParts);

            //if (webParts != null)
            //{
            //    foreach (WebPartNodeInfo webPart in webParts)
            //    {
            //        // Create a new annotation object to store the current Web Part item with the new node.
            //        var annotations = new Dictionary<object, object>() 
            //        { 
            //            { typeof(WebPartNodeInfo), webPart } 
            //        };

            //        // Create the new node for the current Web Part item.
            //        parentNode.ChildNodes.Add(WebPartNodeTypeProvider.WebPartNodeTypeId,
            //            webPart.Name,
            //            annotations);
            //    }
            //}
        }

        #endregion
    }
}
